var app = getApp();
Page({
	data:{
		long:0,
    lat:0,
    center_long:0,
    center_lat:0,
    markers:[],
    controls: [],
    a:"1",//与b一起渲染页面(动态变更class)
    b: "0",
    c:1,//控制商户和收货点的渲染
    d:0,//控制首次经纬度渲染
    data_url: "",
    winWidth: 0,
    winHeight: 0,
    nearby_map_h:0,
    scale: 16,
  }, 
  tabBus:function(e){
    var that = this
    if(that.data.c === 0){
      wx.request({
        url: that.data.data_url + "/GPD/findvmbylatandlng.do",
        data: {
          lng: that.data.center_long,
          lat: that.data.center_lat,
          range: 1000
        },
        success:function(res){
          var map_markers = new Array
          for (var i = 0; i < res.data.length; i++) {
            map_markers[i] = {
              id: res.data[i].id,
              longitude: res.data[i].lng,
              latitude: res.data[i].lat,
              width: "40",
              height: "56",
              iconPath: "/images/nearby_ic_bus_nor.png"
            }
          that.setData({
            markers: map_markers
          })
          }
        }
      })
      that.setData({
        c:1
      })
    }
  },
  tabVm:function(res){
    var that = this
    if (that.data.c === 1) {
      wx.request({
        url: that.data.data_url + "/GPD/findvmbylatandlng.do",
        data: {
          lng: that.data.center_long,
          lat: that.data.center_lat,
          range: 1000
        },
        success: function (res) {
          var map_markers = new Array
          for (var i = 0; i < res.data.length; i++) {
            map_markers[i] = {
              id: res.data[i].id,
              longitude: res.data[i].lng,
              latitude: res.data[i].lat,
              width: "48",
              height: "51",
              iconPath: "/images/nearby_ic_business_nor.png"
            }
          that.setData({
            markers: map_markers
          })
          }
        }
      })
      that.setData({
        c: 0
      })
    }
  },
  controltap(e) {//地图缩放
    var that = this
    if(e.controlId === 1){
      this.data.scale == 18 ? this.data.scale : this.data.scale++
      this.setData({
        long: that.data.center_long,
        lat: that.data.center_lat,
        scale : that.data.scale
        })
    }else if(e.controlId === 2){
      this.data.scale == 0 ? this.data.scale : this.data.scale--
      this.setData({
        long: that.data.center_long,
        lat: that.data.center_lat,
        scale : that.data.scale
      })
      // 下面为添加标记的方法
    }else if(e.controlId === 3){
      wx.request({
        url: "http://192.168.0.115:8080/GPD/addvm.do",
        data:{
          lng:that.data.center_long,
          lat:that.data.center_lat,
        },
        success:function(res){
        }
      })
    }else if(e.controlId ===4){
      wx.getLocation({
        type: "gcj02",
        success: function (res) {
          that.setData({
            long: res.longitude,
            lat: res.latitude
          })
        },
      })
    }
  },
  // 删除标记的方法
  // delmarker(e){
  //   console.log(e)
  //   wx.request({
  //     url: "http://192.168.0.115:8080/GPD/deletevm.do",
  //     data:{
  //       id:e.markerId
  //     },
  //     success(res){
  //       console.log(res)
  //     }
  //   })
  // },
  regionchange(e){//获取中心点坐标
    if(e.type == 'end'){
    this.getCenterLocation()
    }
  },
  getCenterLocation:function(res){
    this.mapCtx = wx.createMapContext('map')
    var that = this
    this.mapCtx.getCenterLocation({
      success: function (res) {
        var long = res.longitude
        var lat = res.latitude
        that.setData({
          center_long:long,
          center_lat:lat,
          long: that.data.center_long.long,
          lat: that.data.center_lat.lat
        })
        wx.request({
          url: that.data.data_url + "/GPD/findvmbylatandlng.do",
          data:{
            lng: that.data.center_long,
            lat: that.data.center_lat,
            range:1000
          },
          success(res){
            var map_markers = [];
            for(var i = 0;i<res.data.length;i++){
              if(that.data.c == 1){
                  map_markers[i] = {
                  id: res.data[i].id,
                  longitude: res.data[i].lng,
                  latitude: res.data[i].lat,
                  width: "40",
                  height: "56",
                  iconPath: "/images/nearby_ic_bus_nor.png"
                }
              } else if (that.data.c == 0){
                  map_markers[i] = {
                  id: res.data[i].id,
                  longitude: res.data[i].lng,
                  latitude: res.data[i].lat,
                  width: "40",
                  height: "50",
                  iconPath: "/images/nearby_ic_business_nor.png"
                }
              }
            }
            that.setData({
              markers: map_markers
            })
          }
        })
      }
    })
  },
	onLoad:function(options){
    var that = this;
    this.setData({
      data_url: app.globalData.data_url
    })
    if(that.data.d == 0){
      that.setData({
        d:1
      })
    wx.getLocation({
      type:"gcj02",
      success: function(res) {
        // if(that.data.long&&that.data.lat){
        // }else{
          that.setData({
            long:res.longitude,
            lat:res.latitude
          })
        // }
      },
    })
    }
    wx.getSystemInfo({//渲染各个按钮
      success: function(res) {
        var map_controls = [{
            id: 1,
            position: { left: res.windowWidth - 60, top: res.windowHeight - 250, width: 40, height: 50 },
            iconPath: "/images/map_add.png",
            clickable: true
        }, {
            id: 2,
            position: { left: res.windowWidth - 60, top: res.windowHeight - 210, width: 40, height: 50 },
            iconPath: "/images/map_down.png",
            clickable: true
          }, {
            id: 3,
            position: { left: res.windowWidth/2-15, top: res.windowHeight/2-54, width: 30, height: 54 },
            iconPath: "/images/ic_pinter.png",
            clickable: true
          }, {
            id: 4,
            position: { left: res.windowWidth - 54, top: res.windowHeight -140, width: 28, height: 28 },
            iconPath: "/images/nearby_ic_loac_nor.png",
            clickable: true
          }
          ]
        that.setData({
          winWidth:res.windowWidth,//获取屏幕宽度
          winHeight: res.windowHeight,//获取屏幕高度
          nearby_map_h: res.windowHeight-44,//设置屏幕高度
          controls:map_controls//设置按钮
        })
      }
    })
  },
	onReady:function(){
      wx.showModal({
        title: '温馨提示',
        content: '为防止您走丢，请开启GPS定位功能，iPhone用户请无视^_^',
        showCancel: false,
        confirmText: '朕知道了',
        confirmColor: '#5fcf40',
      })
	},
	onShow:function(){
    
	},
	onHide:function(){
		
	},
	onUnload:function(){
		
	},
	onPullDownRefresh:function(){
		
	},
	onReachBottom:function(){
		
	}
})		
// map.js
// Page({
//   data: {
//     markers: [{
//       iconPath: "/resources/others.png",
//       id: 0,
//       latitude: 23.099994,
//       longitude: 113.324520,
//       width: 50,
//       height: 50
//     }],
//     polyline: [{
//       points: [{
//         longitude: 113.3245211,
//         latitude: 23.10229
//       }, {
//         longitude: 113.324520,
//         latitude: 23.21229
//       }],
//       color: "#FF0000DD",
//       width: 2,
//       dottedLine: true
//     }],
//     controls: [{
//       id: 1,
//       iconPath: '/images/map_add.png',
//       position: {
//         left: 0,
//         top: 300 - 50,
//         width: 50,
//         height: 50
//       },
//       clickable: true
//     }]
//   },
//   regionchange(e) {
//     console.log(e.type)
//   },
//   markertap(e) {
//     console.log(e.markerId)
//   },
//   controltap(e) {
//     console.log(e)
//   }
// })